package uk.ac.aber.Blockmation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 *
 * @author alex
 */
public class NewProjectDialog extends JDialog implements ChangeListener, ActionListener {
    
    private JSpinner widthSpinner;
    private JSpinner heightSpinner;
    private int dialogWidth = 20;
    private int dialogHeight = 20;
    private Color backgroundColorDialog = Color.white;
    private JButton selectBgColourButton;
    private MainFrame mainFrame;
    
    public NewProjectDialog(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        
        this.setModal(true);
        this.setTitle("Create New Project");
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setSize(370, 110);
        
        JPanel dialogPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout(0,10,10);
        dialogPanel.setLayout(flowLayout);
        
        // Width Spinner
        JLabel widthSpinnerLabel = new JLabel("Width");
        SpinnerNumberModel widthSpinnerModel = new SpinnerNumberModel(20, //initial value
                1, //minimal value
                195, //maximal value
                1); //step
        widthSpinner = new JSpinner(widthSpinnerModel);
        widthSpinner.addChangeListener(this);
        
        // Height Spinner
        JLabel heightSpinnerLabel = new JLabel("Height");
        SpinnerNumberModel heightSpinnerModel = new SpinnerNumberModel(20, //initial value
                1, //minimal value
                195, //maximal value
                1); //step
        heightSpinner = new JSpinner(heightSpinnerModel);
        heightSpinner.addChangeListener(this);
        
        // Select Background Colour Button
        selectBgColourButton = new JButton("BgColour");
        selectBgColourButton.addActionListener(this);
        
        // Go Back Button
        JButton goBackButton = new JButton("Go Back");
        goBackButton.addActionListener(this);
        
        // Create New Project Button
        JButton createNewProjectButton = new JButton("Create New Project");
        createNewProjectButton.addActionListener(this);
        
        
        // Add Components to Panel
        dialogPanel.add(widthSpinnerLabel);
        dialogPanel.add(widthSpinner);
        dialogPanel.add(heightSpinnerLabel);
        dialogPanel.add(heightSpinner);
        dialogPanel.add(selectBgColourButton);
        dialogPanel.add(goBackButton);
        dialogPanel.add(createNewProjectButton);
        
        
        // Add Panel to Dialog
        this.add(dialogPanel);
        this.setVisible(true);
    }
    
    /**
     * Create New Project Dialog Box spinners
     */
    public void stateChanged(ChangeEvent e) {
        dialogWidth = Integer.parseInt(this.widthSpinner.getModel().getValue().toString());
        dialogHeight = Integer.parseInt(this.heightSpinner.getModel().getValue().toString());
    }
    
    /**
     * Create New Project Dialog Box Button Actions
     */
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        
        if (actionCommand.equals("BgColour")) {

        	ColorDialog colorDialog = new ColorDialog(this);
        	backgroundColorDialog = colorDialog.getSelectedColor();
        	selectBgColourButton.setBackground(backgroundColorDialog);
                        
        }
        else if (actionCommand.equals("Go Back")) {
            this.setVisible(false);
            this.dispose();
        }
        else if (actionCommand.equals("Create New Project")) {

            Dimension frameDim = new Dimension(dialogWidth, dialogHeight);

            BufferedImage bim = new BufferedImage(frameDim.width,
                    frameDim.height, BufferedImage.TYPE_INT_RGB);
           
            mainFrame.getPad().setImage(bim);
            mainFrame.getPad().setBackgroundColor(backgroundColorDialog);
            mainFrame.getPad().clear();

            mainFrame.newMovieModel();
            mainFrame.getMovieModel().appendFrame( mainFrame.getPad().getImage() );

            mainFrame.updateAppWithNewModel();
            mainFrame.setChangesSavedToFile(false);
            
            this.setVisible(false);
            this.dispose();
        }
        else {
            System.err.println("Error! Unexpected ActionCommand.");
	}
    }

	public void setBackgroundColorDialog(Color backgroundColorDialog) {
		this.backgroundColorDialog = backgroundColorDialog;
	}
}
